package linkedlist;

/**
 * Created by JiaJia on 2019/6/29 0029.
 */
public class IntersectionofTwo {
    private static class ListNode {
             int val;
             ListNode next = null;
              ListNode(int val) {
                      this.val = val;
                  }

        @Override
        public String toString() {
            return val+" ";
        }
    }
    private static ListNode getIntersectionNode(ListNode headA,ListNode headB){
        ListNode l1=headA,l2=headB;
        while (l1!=l2){
            l1=(l1==null)?headB:l1.next;
            l2=(l2==null)?headA:l2.next;
        }
        return l1;
    }

    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);

        n1.next = n2;
        n2.next = n3;
        n3.next = n6;
        n6.next = n7;

        n4.next = n5;
        n5.next = n6;
        System.out.println(getIntersectionNode(n1,n4));
    }
}